package com.yh.hljapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.yh.hljapp.AppContext;
import com.yh.hljapp.api.RetrofitSingleton;
import com.yh.hljapp.base.BaseListFragment;
import com.yh.hljapp.common.PLog;
import com.yh.hljapp.common.StringUtils;
import com.yh.hljapp.common.UIHelper;
import com.yh.hljapp.modules.adatper.CustPhoneHGZAdapter;
import com.yh.hljapp.modules.adatper.ListBaseAdapter;
import com.yh.hljapp.modules.bean.CustPhoneHGZ;
import com.yh.hljapp.modules.bean.HGZBean;
import com.yh.hljapp.modules.bean.SimpleBackPage;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.yh.hljapp.gddb.gdbean.QcodeDao.Properties.FBase;
import static com.yh.hljapp.gddb.gdbean.QcodeDao.Properties.FWeight;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：BackMatListFragment
 * <p>
 * 创建时间：2017.06.02.15:30
 * *****************************
 */
public class AddHGZFragment extends BaseListFragment<CustPhoneHGZ> {

    private String mType;
    private String name = "all";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mType = bundle.getString("type");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        img_shaixuan.setVisibility(View.GONE);
    }

    @Override
    protected ListBaseAdapter getListAdapter() {
        return new CustPhoneHGZAdapter(mType);
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ll_search.setVisibility(View.VISIBLE);
        img_shaixuan.setVisibility(View.VISIBLE);
        et_earch.setHint("名称模糊查询");
        leftDate = new String[]{"名称查询"};
    }

    @Override
    protected void sendRequestData(String id) {
        super.sendRequestData(id);
        showWaitDialog();
        RetrofitSingleton.getApiService(mActivity).
                OrgHGZAll(name, mCurrentPage, AppContext.PAGE_SIZE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

        name = "all";
    }

    String mKey = "model", mName = "名称模糊查询";

    @Override
    public void inputAction(String str) {
        showWaitDialog();
        mCurrentPage = 1;
        mAdapter.clear();
        if (!StringUtils.isEmpty(str)) {
            name = str;
        }
        showWaitDialog();
        sendRequestData(null);
    }

    @Override
    public void searchAction(int position) {
        //"门编号", "错误", "执行日期", "提交日期", "工序" “购物”
        mName = leftDate[position];
        switch (position) {
            case 0:
                et_earch.setHint(leftDate[position] + "模糊查询");
                mKey = "model";
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        super.onItemClick(parent, view, position, id);
        CustPhoneHGZ item = mAdapter.getItem(position);
        if (item != null) {
            HGZBean bean = new HGZBean();

            String model = item.getModel();    //生产日期
            String guige = item.getGuige();  //规格型号
            String jiTai = item.getJiTai();
            String jTou = item.getJTou();
            String zhi_jing = item.getZhi_Jing();

            String name = item.getName(); //产品名称
            String banZhu = item.getBanZhu();
            String chanpincode = item.getChanpincode();

            String number = item.getNumber(); //单据编号
            String fbase = item.getFbase();//产品代码id
            String fWeight = item.getFWeight(); //重量   这3个需要生成json
//            {"FBillNumber":"YZ20180117633","FBase":"1001","FWeight":"66"}
            if (!StringUtils.isEmpty(name) && !StringUtils.isEmpty(chanpincode) && !StringUtils.isEmpty(fbase)
                    && !StringUtils.isEmpty(fWeight)) {

                if (fWeight.contains(".")) {
                    String fWeights = fWeight.substring(0, fWeight.indexOf("."));
                    bean.setFWeight(fWeights);
                } else {
                    bean.setFWeight(fWeight);
                }
                bean.setFBase(fbase);
                bean.setFBillNumber(number);


                Gson gson = new Gson();
                String json = gson.toJson(bean);


                JSONObject jObject = new JSONObject();
                try {
                    //设置的ip打印地址
                    String set_ip = AppContext.getInstance().getProperty("set_ip");
                    if (!StringUtils.isEmpty(set_ip)) {
                        jObject.put("hostUrl", "http://" + set_ip + "/CLodopfuncs.js");
                        jObject.put("opendirection", "http://" + set_ip + "/images/arrow_right.png");
                    } else {
                        jObject.put("hostUrl", "http://192.168.0.170:8000/CLodopfuncs.js");
                        jObject.put("opendirection", "http://192.168.0.170:8000/images/arrow_right.png");
                    }
                    //赋值
                    jObject.put("title", "护理佳产品合格证");
                    jObject.put("cpdm", chanpincode);
                    jObject.put("cpmc", name);
                    jObject.put("ggxh", guige);
                    jObject.put("jietou", jTou + "");
                    jObject.put("zj", zhi_jing + "");

                    if (banZhu.contains("*")) {
                        String banZHus = banZhu.replace("*", "");
                        jObject.put("bz", banZHus);
                    } else {
                        jObject.put("bz", banZhu);
                    }

                    if (jiTai.contains("*")) {
                        String jiTais = jiTai.replace("*", "");
                        jObject.put("jt", jiTais);
                    } else {
                        jObject.put("jt", jiTai);
                    }

                    if (model.contains("/")) {
                        String models = model.replace("/", "-");
                        jObject.put("date", models);
                    } else {
                        jObject.put("date", model);
                    }

                    if (fWeight.contains(".")) {
                        String fWeights = fWeight.substring(0, fWeight.indexOf("."));
                        jObject.put("amount", fWeights);
                    } else {
                        jObject.put("amount", fWeight);
                    }
                    jObject.put("address", "河南省鹿邑县产业集聚区");
                    jObject.put("tax", "0394-7490258");
                    jObject.put("phone", "0394-7490998");
                    jObject.put("web", "WWW.hulijia.com");
                    jObject.put("qrcode", json);
                    jObject.put("code", "" + number);
                    String Fstring = jObject.toString();

                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("fB", Fstring);
                    intent.putExtras(bundle);
                    UIHelper.showSimpleBack(mActivity, SimpleBackPage.WEB, bundle, "打印预览");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Snackbar.make(mActivity.getWindow().getDecorView(), "请确认合格证是否选择正确！", Snackbar.LENGTH_SHORT).show();
            }
        } else {
            Snackbar.make(mActivity.getWindow().getDecorView(), "当前列表获取失败，请联系管理员！", Snackbar.LENGTH_SHORT).show();
        }
    }
}
